import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutterjc/plugin/provide/ProvideFactory.dart';
import 'package:provide/provide.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(new UrlLuanchPlugin());
}

/*
 * UrlLuanch组件
 * https://www.jianshu.com/p/cab2c1c2523f：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class UrlLuanchPlugin extends StatefulWidget {
  State<StatefulWidget> createState() {
    return new UrlLuanchPluginState();
  }
}

/*
 * 页面功能 <br/>
 */
class UrlLuanchPluginState extends State<StatefulWidget> {
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {},
      ),
      title: new Text('FlutterDesign'),
      centerTitle: true,
      actions: <Widget>[
        new Container(width: 0, height: 0),
        new Container(width: 0, height: 0),
      ],
      backgroundColor: Colors.blue,
    );

    return MaterialApp(
      home: new Scaffold(
        appBar: appBar,
        body: new Builder(builder: (BuildContext context) {
          return Column(
            children: <Widget>[
              RaisedButton(
                child: Text('跳转'),
                onPressed: () {
                  updateDownloadApp(
                      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1587144931405&di=1efbdae511d97285db9cc0b64ea54357&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fnews%2Fw%3D638%2Fsign%3D0864a41a0ad79123e0e0977795355917%2Fd4628535e5dde711520bb7e4a1efce1b9c1661c3.jpg');
                },
              ),
            ],
          );
        }),
        backgroundColor: Colors.white,
        bottomNavigationBar: null,
      ),
    );
  }

  /*
   * 更新下载app
   * @param AppUrl app下载路径
   */
  void updateDownloadApp(String appUrl) async {
    if (await canLaunch(appUrl)) {
      await launch(appUrl);
    } else {
      print(appUrl);
    }
  }
}
